package com.axter.libs.photo.display;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaobo on 2017/6/1.
 */

public class SimplePhotoDisplay implements IPhotoDisplayInterface {
    private List<LoadingListener> loadingListeners = new ArrayList<>();
    private List<LoadingProgressListener> loadingProgressListeners = new ArrayList<>();
    private ImageLoadingListener loadingListener = new ImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            for (LoadingListener loadingListener : loadingListeners) {
                loadingListener.onLoadingStarted(imageUri, view);
            }
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            for (LoadingListener loadingListener : loadingListeners) {
                loadingListener.onLoadingFailed(imageUri, view, failReason);
            }
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            for (LoadingListener loadingListener : loadingListeners) {
                loadingListener.onLoadingComplete(imageUri, view, loadedImage);
            }
        }

        @Override
        public void onLoadingCancelled(String imageUri, View view) {
            for (LoadingListener loadingListener : loadingListeners) {
                loadingListener.onLoadingCancelled(imageUri, view);
            }
        }
    };
    private ImageLoadingProgressListener loadingProgressListener = new ImageLoadingProgressListener() {
        @Override
        public void onProgressUpdate(String imageUri, View view, int current, int total) {
            for (LoadingProgressListener progressListener : loadingProgressListeners) {
                progressListener.onProgressUpdate(imageUri, view, current, total);
            }
        }
    };

    public void displayUrl(String url, ImageView imageview, LoadingListener listener, LoadingProgressListener progressListener) {
        if (listener != null && !loadingListeners.contains(listener)) {
            loadingListeners.add(listener);
        }
        if (progressListener != null && !loadingProgressListeners.contains(progressListener)) {
            loadingProgressListeners.add(progressListener);
        }
        if (url.startsWith("/")) {
            ImageLoader.getInstance().displayImage("file://" + url, imageview, getNetOptions(), loadingListener, loadingProgressListener);
        } else {
            ImageLoader.getInstance().displayImage(url, imageview, getNetOptions(), loadingListener, loadingProgressListener);
        }
    }

    public void displayLocale(String path, ImageView imageView, int width, int height, int resid) {
        ImageLoader.getInstance().displayImage("file://" + path, new ImageViewAware(imageView), getLocalOptions(resid), new ImageSize(width, height), null, null);
    }

    @Override
    public void clearMemoryCache() {
        ImageLoader.getInstance().getMemoryCache().clear();
    }

    public String getLocalPath(String imageUri) {
        return ImageLoader.getInstance().getDiskCache().get(imageUri).getAbsolutePath();
    }

    private static DisplayImageOptions localOptions;
    private static DisplayImageOptions netOptions;

    private static DisplayImageOptions getNetOptions() {
        if (netOptions == null) {
            netOptions = new DisplayImageOptions.Builder()
                    .cacheOnDisk(true)
                    .cacheInMemory(true)
                    .build();
        }
        return netOptions;
    }

    private static DisplayImageOptions getLocalOptions(int resid) {
        if (localOptions == null) {
            localOptions = new DisplayImageOptions.Builder()
                    .showImageOnLoading(resid)
                    .showImageForEmptyUri(resid)
                    .cacheInMemory(true)
                    .build();
        }
        return localOptions;
    }
}
